#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
        vector<int> v1;
        list<int> l1(arr1.begin(), arr1.end());
        for (auto& e : arr2)
        {
            list<int>::iterator t = l1.begin();
            while (t != l1.end())
            {
                if (*t == e)
                {
                    v1.push_back(e);
                    t = l1.erase(t);
                }
                else
                    ++t;
            }
        }
        vector<int> v2(l1.begin(), l1.end());
        sort(v2.begin(), v2.end());
        for (auto& e : v2)
            v1.push_back(e);

        return v1;
    }
};